<?php
/**
 * 权限相关model
 * @date 2012-06-19
 * @author zhaorongwei
 */
class AuthModel extends Model
{
	function __construct()
	{
		parent::__construct();
	}

	/**
	 * 增加方法
	 * @param array $data 加入数据
	 * @return int -1:方法名已存在,>0: 插入成功 
	 */
	public function functionAdd($data)
	{
		$data = post('data');
		$funcid=$this->db->insertData('function_tree',$data);
		if($funcid)
		{
			//开发组和admin组自动加上新增模块的功能
			$this->db->query("UPDATE user_group SET ffunctionids=concat(ffunctionids,$funcid,',') WHERE fid<3");
		}
		return $funcid;
	}

	/**
	 * 判断方法是否存在
	 * @param int parentid 父id
	 * @param string $method 方法名称
	 * @return bool 
	 */
	public function functionExists($parentid,$method)
	{
		return $this->db->getVar("SELECT fid FROM function_tree WHERE fparent_id=".intval($parentid)." AND fname_en='".$this->db->escape($method)."'")>0;
	}	

	/**
	 * 修改方法内容
	 * @param int $id 方法id
	 * @param array $data
	 * @return int
	 */
	public function functionEdit($id,$data)
	{
		$id=intval($id);
		if($id<1 || !$data)
		{
			return false;
		}
		return  $this->db->updateData('function_tree', $data, array('fid'=>$id));
	}

	/**
	 * 递归删除数据,慎用
	 * @param int $id 被删除的数据ID
	 */
	public function functionDel($id)
	{
		$id=intval($id);
		if($id>0)
		{
			$arr=$this->db->getResults("SELECT fid FROM function_tree WHERE fparent_id=$id");
			$this->db->query("DELETE FROM function_tree WHERE fid=$id");
			if($arr)
			{
				foreach($arr as $k=>$v)
				{
					$this->functionDel($v['fid']);
				}
			}
		}
	}

	/**
	 * 获取方法列表
	 * @param bool $only_show 是否只显示非隐藏方法
	 * @return array
	 */
	public function functionList($only_show = false)
	{
		$sql = 'SELECT * FROM function_tree';
		if($only_show) $sql .= ' WHERE `is_show` = 1';
		$sql.=' ORDER BY forder_num ASC,fid ASC';
		$rt = $this->db->getResults($sql);
		return $rt;
	}
	
	/**
	 * 获取授权的方法数组
	 */
	public function authFunctionList($fids)
	{
		$fids = trim($fids, ',');
		$sql = 'SELECT * FROM function_tree WHERE `fid` IN('.$fids.') AND `fis_show` = 1 ORDER BY forder_num ASC,fid ASC';
		$rt = $this->db->getResults($sql);
		return $rt;
	}
	
	public function getFunctionById($id)
	{
		return $this->db->getRow('SELECT * FROM function_tree WHERE fid = '.intval($id));
	}

	//获得用户授权模块
	public function getAuthByUid($uid)
	{
		$rtn=array();
		$sql="SELECT g.ffunctionids FROM user_group g,user u WHERE u.fid=$uid AND u.fgroupid=g.fid";
		$ids=$this->db->getVar($sql);
		if($ids)
		{
			$ids=trim($ids,',');
			$rtn['fids']=explode(',',$ids);
			$funcs=$this->db->getResults("SELECT fname_en FROM function_tree WHERE fid in($ids)");
			$len=count($funcs);
			for($i=0;$i<$len;$i++)
			{
				$rtn['func'][]=$funcs[$i]['fname_en'];
			}
		}
		return $rtn;
	}

	/*
	 * 判断用户是否有方法权限
	 * @param int $uid 用户id
	 * @param string $function_name
	 * @return bool
	 */
	public function isAuthUser($uid, $function_name)
	{
		$sql = "SELECT fid FROM function_tree WHERE `fname_en` = '".$this->db->escape($function_name)."'";
		$funcid = $this->db->getVar($sql);
		$rt=$this->db->getVar("SELECT g.ffunctionids FROM user_group g,user u WHERE u.fid=$uid and u.fgroupid=g.fid");
		$rt=trim($rt,',');
		if($rt)
		{
			$arr=explode(',',$rt);
			return in_array($funcid,$arr);
		}
		return false;
	}
}
